package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.constant.PurchaseConstant;
import com.atguigu.common.constant.PurchaseDetailConstant;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;
import com.atguigu.gulimall.ware.service.WareSkuService;
import com.atguigu.gulimall.ware.vo.PurchaseDoneStatusVo;
import com.atguigu.gulimall.ware.vo.PurchaseDoneVo;
import com.atguigu.gulimall.ware.vo.PurchaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.PurchaseDao;
import com.atguigu.gulimall.ware.entity.PurchaseEntity;
import com.atguigu.gulimall.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {
    @Autowired
    private PurchaseDetailService purchaseDetailService;
    @Autowired
    private WareSkuService wareSkuService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryUnreceiveList(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status", PurchaseConstant.StatusEnum.CREATE.getCode()).or().eq("status",PurchaseConstant.StatusEnum.ASSIGNED.getCode())
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void merge(PurchaseVo purchaseVo) {
        List<Long> items = purchaseVo.getItems();
        Long purchaseId = purchaseVo.getPurchaseId();
        if(purchaseId==null){
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(PurchaseConstant.StatusEnum.CREATE.getCode());
            purchaseEntity.setPriority(1);
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }
        for (int i = 0; i < items.size() ; i++) {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(items.get(i));
            purchaseDetailEntity.setPurchaseId(purchaseId);
            purchaseDetailEntity.setStatus(PurchaseDetailConstant.StatusEnum.ASSIGNED.getCode());
            purchaseDetailService.updateById(purchaseDetailEntity);
        }
        if(purchaseId!=null){
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setId(purchaseId);
            this.updateById(purchaseEntity);
        }
    }

    @Override
    @Transactional
    public void received(List<Long> purchaseIds) {
        purchaseIds.forEach(purchaseId->{
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setId(purchaseId);
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(PurchaseConstant.StatusEnum.PURCHASEING.getCode());
            this.updateById(purchaseEntity);

            List<PurchaseDetailEntity> purchaseDetailEntities = purchaseDetailService.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", purchaseId));            purchaseDetailEntities.forEach(purchaseDetailEntity -> {
                purchaseDetailEntity.setStatus(PurchaseDetailConstant.StatusEnum.PURCHASEING.getCode());
                purchaseDetailService.updateById(purchaseDetailEntity);
            });
        });
    }

    @Override
    @Transactional
    public void done(PurchaseDoneVo purchaseDoneVo) {
        //改变采购单状态
        Long purchaseId = purchaseDoneVo.getId();
        List<PurchaseDoneStatusVo> items = purchaseDoneVo.getItems();
        boolean done = true;
        for (int i = 0; i < items.size(); i++) {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            purchaseDetailEntity.setId(items.get(i).getItemId());
            purchaseDetailEntity.setStatus(items.get(i).getStatus());
            if(items.get(i).getStatus()==PurchaseDetailConstant.StatusEnum.HAS_ERROR.getCode()){
                done = false;
            }else if (items.get(i).getStatus()==PurchaseDetailConstant.StatusEnum.FINISHED.getCode()){
                PurchaseDetailEntity entity = purchaseDetailService.getById(items.get(i).getItemId());
                wareSkuService.addToWare(entity.getSkuId(),entity.getWareId(),entity.getSkuNum());
            }
            purchaseDetailService.updateById(purchaseDetailEntity);
        }

        //改变采购需求状态
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setStatus(done?PurchaseConstant.StatusEnum.FINISHED.getCode():PurchaseConstant.StatusEnum.HAS_ERROR.getCode());
        purchaseEntity.setUpdateTime(new Date());
        purchaseEntity.setId(purchaseId);
        this.updateById(purchaseEntity);

    }

}